﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;

namespace CopySheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! Please Wait while processing...");
            //Excel worksheet combine example
            //Note: This example does not check for duplicate sheet names. Your test files should have different sheet names.

            using (IWorkbook book1 = new XSSFWorkbook(new FileStream("file1.xlsx", FileMode.Open)))
            {
                using (IWorkbook book2 = new XSSFWorkbook(new FileStream("file2.xlsx", FileMode.Open)))
                {
                    using (IWorkbook product = new XSSFWorkbook())
                    {

                        for (int i = 0; i < book1.NumberOfSheets; i++)
                        {
                            ISheet sheet1 = book1.GetSheetAt(i);
                            sheet1.CopyTo(product, sheet1.SheetName, true, true);
                        }

                        for (int j = 0; j < book2.NumberOfSheets; j++)
                        {
                            ISheet sheet2 = book2.GetSheetAt(j);
                            sheet2.CopyTo(product, sheet2.SheetName, true, true);
                        }

                        using (FileStream sw = File.Create("test.xlsx"))
                        {
                            product.Write(sw, false);
                        }
                    }
                }
            }
        }
    }
}
